/*
 *    Copyright 2008-2009 The PoCo Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.asemantics.poco.converters;

import java.sql.Date;
import java.util.TimeZone;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import com.asemantics.poco.DateTime;
import com.asemantics.poco.Drinker;
import com.asemantics.poco.Gender;
import com.asemantics.poco.MediaItemType;
import com.asemantics.poco.Smoker;
import com.asemantics.poco.query.FilterOp;
import com.asemantics.poco.query.Format;
import com.asemantics.poco.query.SortOrder;

/**
 * 
 * @author Simone Tripodi
 * @version $Id: PoCoConverterUtils.java 247 2009-12-20 00:44:30Z simone.tripodi@gmail.com $
 */
public final class PoCoConverterUtils {

    private static Boolean CONVERTERS_INITIALIZED = Boolean.FALSE;

    public static void checkAndRegisterConverters() {
        synchronized (CONVERTERS_INITIALIZED) {
            if (!CONVERTERS_INITIALIZED) {
                DateConverter dateConverter = new DateConverter(null);
                dateConverter.setPattern("yyyy-MM-dd");
                ConvertUtils.register(dateConverter, Date.class);

                ConvertUtils.register(new TimeZoneConverter(), TimeZone.class);
                ConvertUtils.register(new DateTimeConverter(), DateTime.class);

                for (Class<?> clazz : new Class<?>[] {
                        Gender.class,
                        Drinker.class,
                        FilterOp.class,
                        Format.class,
                        MediaItemType.class,
                        SortOrder.class,
                        Smoker.class }) {
                    ConvertUtils.register(new EnumConverter(clazz), clazz);
                }

                CONVERTERS_INITIALIZED = Boolean.TRUE;
            }
        }
    }

    private PoCoConverterUtils() {
        // do nothing
    }

}
